import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:ducafe_ui_core/ducafe_ui_core.dart';

import 'package:lmmi/view/home/page.dart';
import 'package:lmmi/view/home/widgets/calendar_bottom_sheet.dart';

void main() {
  group('HomePage Calendar Functionality Tests', () {
    testWidgets('Calendar button should open calendar bottom sheet', (
      WidgetTester tester,
    ) async {
      await tester.pumpWidget(
        ProviderScope(
          child: ScreenUtilInit(
            designSize: const Size(375, 812),
            child: MaterialApp(home: HomePage()),
          ),
        ),
      );

      // Find the calendar button by looking for GestureDetector with calendar functionality
      final calendarButtons = find.byType(GestureDetector);
      expect(calendarButtons, findsWidgets);

      // Tap on the calendar button (first GestureDetector should be the calendar button)
      await tester.tap(calendarButtons.first);
      await tester.pumpAndSettle();

      // Verify that the calendar bottom sheet is displayed
      expect(find.byType(CalendarBottomSheet), findsOneWidget);
    });

    testWidgets('Date items should show selected state when tapped', (
      WidgetTester tester,
    ) async {
      await tester.pumpWidget(
        ProviderScope(
          child: ScreenUtilInit(
            designSize: const Size(375, 812),
            child: MaterialApp(home: HomePage()),
          ),
        ),
      );

      // Wait for the app to settle
      await tester.pumpAndSettle();

      // Find date items in the horizontal scroll list
      final dateItems = find.byType(GestureDetector);
      expect(dateItems, findsWidgets);

      // Tap on a date item to test selection
      if (dateItems.evaluate().length > 1) {
        await tester.tap(dateItems.at(1));
        await tester.pumpAndSettle();

        // Verify that no exceptions occurred
        expect(tester.takeException(), isNull);
      }
    });

    testWidgets('Future dates should not be selectable', (
      WidgetTester tester,
    ) async {
      await tester.pumpWidget(
        ProviderScope(
          child: ScreenUtilInit(
            designSize: const Size(375, 812),
            child: MaterialApp(home: HomePage()),
          ),
        ),
      );

      await tester.pumpAndSettle();

      // This test verifies that the UI doesn't crash when trying to interact with future dates
      // The actual visual verification would require more complex testing setup
      expect(find.byType(HomePage), findsOneWidget);
    });

    testWidgets('Calendar should display current month and year', (
      WidgetTester tester,
    ) async {
      await tester.pumpWidget(
        ProviderScope(
          child: ScreenUtilInit(
            designSize: const Size(375, 812),
            child: MaterialApp(home: HomePage()),
          ),
        ),
      );

      // Tap calendar button to open calendar
      final calendarButtons = find.byType(GestureDetector);
      await tester.tap(calendarButtons.first);
      await tester.pumpAndSettle();

      // Verify calendar is displayed
      expect(find.byType(CalendarBottomSheet), findsOneWidget);

      // Verify that navigation arrows are present
      expect(find.byIcon(Icons.chevron_left), findsOneWidget);
      expect(find.byIcon(Icons.chevron_right), findsOneWidget);
    });
  });
}
